
/**
 * entity图层
 */
import layerType from '../layerType'
import { v4 as uuid } from 'uuid'
import Layer from '../layer'

export default class PrimitiveLayer extends Layer {

  /**
  * 
  * @param {*} viewer viewer场景对象
  * @description 创建图层组 并添加至viewer场景中 代理show 创建对应图层事件
  */

  constructor(viewer) {
    super()
    this._id = uuid()
    this._viewer = viewer
    this.collection = new Cesium.PrimitiveCollection(layerType.PRIMITIVE_LAYER)
    this._points = this.collection.add(new Cesium.PointPrimitiveCollection())
    this._labels = this.collection.add(new Cesium.LabelCollection())
    this._billboards = this.collection.add(new Cesium.BillboardCollection())
    this._show = true
    this._viewer.scene.primitives.add(this.collection)
  }

  /**
   * 向不同的集合中增加对应元素
   */
  dispatchAdd(primitive) {
    switch (primitive._type) {
      case 'billboard_primitive':
        let billboard = this._billboards.add(primitive.delegate)
        this._cache[primitive.id] = billboard
        break
      case 'label_primitive':
        let label = this._labels.add(primitive.delegate)
        this._cache[primitive.id] = label
        break
      case 'point_primitive':
        let point = this._points.add(primitive.delegate)
        this._cache[primitive.id] = point
        break
      default:
        this.collection.add(primitive.delegate)
        this._cache[primitive.id] = primitive
        break
    }

    primitive.emit()
  }

  /**
   * 向不同的集合中删除对应元素
   */
  dispatchRemove(primitive) {
    switch (primitive._type) {
      case 'billboard_primitive':
        this._billboards.remove(this._cache[primitive.id])
        break;
      case 'label_primitive':
        this._labels.remove(this._cache[primitive.id])
        break;
      case 'point_primitive':
        this._points.remove(this._cache[primitive.id])
        break;
      default:
        this.collection.remove(primitive.delegate)
        break;
    }

    primitive._timer && (primitive._timer = null)
    delete this._cache[primitive.id]

  }

  /**
  * 往集合中增加一个primitive元素
  */

  add(primitive) {
    if (!primitive) return
    primitive._layer = this
    this.dispatchAdd(primitive)
  }

  /**
   * add primitivies to collection
   * @param {*} primitivies 
   */
  adds(primitivies) {
    if (Array.isArray(primitivies)) {
      primitivies.forEach(primitive => {
        this.add(primitive)
      })
    }
  }

  /**
  * 删除集合中的某个元素
  */
  remove(primitive) {
    if (!primitive) return
    this.dispatchRemove(primitive)
    primitive._layer = undefined
  }

  /**
  * 删除集合中的所有元素
  */

  removeAll() {
    this._cache = {}
    this.collection.removeAll()
    this._points = this.collection.add(new Cesium.PointPrimitiveCollection())
    this._labels = this.collection.add(new Cesium.LabelCollection())
    this._billboards = this.collection.add(new Cesium.BillboardCollection())
  }

  /**
  * 销毁primitives中的collection
  */
  destroy() {
    this.removeAll()
    this.viewer.scene.primitives.remove(this.collection)
  }

  get show() {
    return this._show
  }

  set show(val) {
    this._show = val
    this.collection.show = this._show
  }

  get id() {
    return this._id
  }

  get points() {
    return this._points
  }

  get labels() {
    return this._labels
  }

  get billboards() {
    return this._billboards
  }

  get viewer(){
    return this._viewer
  }
}